/**
 * 仓库
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.repository;


import com.sxf.crm.entity.Contact;
import com.sxf.crm.enums.DeleteStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ContactRepository extends JpaRepository<Contact, Long> {
    @Query("SELECT c FROM Contact c WHERE " +
           "(:name IS NULL OR c.name LIKE %:name%) AND " +
           "c.deleteStatus = :deleteStatus")
    Page<Contact> findAll(
        @Param("name") String name,
        @Param("deleteStatus") DeleteStatus deleteStatus,
        Pageable pageable
    );

    @Query("SELECT c FROM Contact c WHERE " +
           "c.customer.id = :customerId AND " +
           "c.deleteStatus = :deleteStatus")
    Page<Contact> findByCustomerId(
        @Param("customerId") Long customerId,
        @Param("deleteStatus") DeleteStatus deleteStatus,
        Pageable pageable
    );
}
